﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Moq;
using Ninject;
using SportsStore.Domain.Entities;
using SportsStore.Domain.Concrete;
using SportsStore.Domain.Abstract;

namespace SportsStore.WebUI.Infrastructure
{
    public class NinjectControllerFactory: DefaultControllerFactory
    {
        public NinjectControllerFactory()
        {
            _ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(
            System.Web.Routing.RequestContext requestContext, 
            Type controllerType)
        {
            return controllerType == null
                       ? null
                       : (IController) _ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {

            _ninjectKernel
                .Bind<IProductsRepository>()
                .To<EFProductRepository>();

            var emailSettings =
                new EmailSettings
                    {
                        WriteAsFile =
                            bool.Parse(ConfigurationManager.AppSettings["Email.WriteAsFile"] ?? "false")
                    };
            _ninjectKernel
                .Bind<IOrderProcessor>()
                .To<EmailOrderProcessor>()
                .WithConstructorArgument("settings", emailSettings);
        }

        private readonly IKernel _ninjectKernel;
    }
}